﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO = Microsoft.MSE.Catalog.Contracts;
using BUS = Microsoft.MSE.Catalog.Module.Business;
using DataAccess = Microsoft.MSE.Catalog.Module.Data;
using System.Data.SqlClient;

namespace Microsoft.MSE.Catalog.Module
{
    public static class ChangeTrackingHelper
    {
        public static DTO.RepositoryChangeSet GetChangeSetForBindingUpdate(DTO.MSEBinding bnd, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersBindingUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {

                //Get event data identifying impact scope (RuntimeServer, Endpoint, Operation)
                List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForBindingID(bnd.ID, txn, conn);
                FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);

                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(rsEvents);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for Binding update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return repositoryChangeSet;
        }
        public static DTO.RepositoryChangeSet GetChangeSetForBindingElementUpdate(DTO.MSEBindingElement bindingElement, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersBindingUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {
                //identify where bindingelement is used
                DTO.MSEBindingList bList = DataAccess.Binding.ListMSEBindingsForBindingElementID(bindingElement.ID, txn, conn);
                if (bList != null && bList.MSEBindings != null)
                {
                    foreach (DTO.MSEBindingListElement binding in bList.MSEBindings)
                    {
                        List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForBindingID(binding.ID, txn, conn);
                        FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);
                    }
                }
                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
                repositoryChangeSet.Items.Add(rsEvents);


            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for BindingElement update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }
            return repositoryChangeSet;
        }

        public static DTO.DBEventContext GetEventContextForEndpointUpdate(DTO.Endpoint endpoint, SqlTransaction txn, SqlConnection conn)
        {
            //get list of runtimes affected
            DTO.DBEventContext eventContext = null;
            try
            {
                List<DTO.DBEventDataItem> eventData = null;
                eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForEndpointID(endpoint.ID, txn, conn);
                eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
                eventContext.EventData = eventData;
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for Endpoint update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }
            return eventContext;
        }

        public static DTO.DBEventContext GetEventContextForRuntimeServerRemoveEndpoints(Guid runtimeServerID, SqlTransaction txn, SqlConnection conn)
        {
            //get list of runtimes affected
            DTO.DBEventContext eventContext = null;
            try
            {
                //get list of endpoints that will be removed
                DTO.EndpointList epList = BUS.EndpointManager.ListEndpointsForRuntimeServerID(runtimeServerID, txn, conn);

                List<DTO.DBEventDataItem> eventData = new List<DTO.DBEventDataItem>();
                epList.Endpoints.ForEach(item => { eventData.Add(new DTO.DBEventDataItem(runtimeServerID, item.ID)); });

                eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Delete);
                eventContext.EventData = eventData;
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for RemoveEndpoints.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }
            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForRuntimeServerRemoveEndpoint(Guid runtimeServerID, Guid endpointID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = null;
            try
            {
                //create change record
                List<DTO.DBEventDataItem> eventData = new List<DTO.DBEventDataItem>();
                eventData.Add(new DTO.DBEventDataItem(runtimeServerID, endpointID));
                eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Delete);
                eventContext.EventData = eventData;
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for RemoveEndpoint.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }
            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForRuntimeServerAssignEndpoint(Guid runtimeServerID, Guid endpointID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = null;

            List<DTO.DBEventDataItem> eventData = new List<DTO.DBEventDataItem>();
            eventData.Add(new DTO.DBEventDataItem(runtimeServerID, endpointID));
            eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Create);
            eventContext.EventData = eventData;
            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForRuntimeServerUpdate(Guid runtimeServerID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = null;

            List<DTO.DBEventDataItem> eventData = new List<DTO.DBEventDataItem>();
            eventData.Add(new DTO.DBEventDataItem(runtimeServerID));
            eventContext = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersUpdated, DTO.DBChangeType.Update);
            eventContext.EventData = eventData;
            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForEndpointPublishOperation(Guid endpointID, Guid operationVersionID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointOpPublicationUpdated, DTO.DBChangeType.Create);
            DTO.OperationVersion opver = null;

            try{
            List<DTO.DBEventDataItem> eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForEndpointID(endpointID, txn, conn);
            if (eventData != null)
            {
                opver = BUS.OperationManager.GetOperationVersion(operationVersionID, txn, conn);
                eventData.ForEach(item => { item.OperationId = opver == null ? Guid.Empty : opver.OperationID; });
                eventContext.EventData = eventData;
            }
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for EndpointPublishOperation.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForEndpointUnPublishOperation(Guid endpointID, Guid operationVersionID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointOpPublicationUpdated, DTO.DBChangeType.Delete);
            DTO.OperationVersion opver = null;

            try
            {
                List<DTO.DBEventDataItem> eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForEndpointID(endpointID, txn, conn);
                if (eventData != null)
                {
                    opver = BUS.OperationManager.GetOperationVersion(operationVersionID, txn, conn);
                    eventData.ForEach(item => { item.OperationId = opver == null ? Guid.Empty : opver.OperationID; });
                    eventContext.EventData = eventData;
                }
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for EndpointUnPublishOperation.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForOperationAssociationRemove(Guid endpointID, Guid operationVersionID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointOpAssociationUpdated, DTO.DBChangeType.Delete);
            DTO.OperationVersion opver = null;

            try
            {
                List<DTO.DBEventDataItem> eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForEndpointID(endpointID, txn, conn);
                if (eventData != null)
                {
                    opver = BUS.OperationManager.GetOperationVersion(operationVersionID, txn, conn);
                    eventData.ForEach(item => { item.OperationId = opver == null ? Guid.Empty : opver.OperationID; });
                    eventContext.EventData = eventData;
                }
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for OperationAssociationRemove.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForOperationAssociationAdded(Guid endpointID, Guid operationVersionID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.EndpointOpAssociationUpdated, DTO.DBChangeType.Create);
            DTO.OperationVersion opver = null;

            try
            {
                List<DTO.DBEventDataItem> eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForEndpointID(endpointID, txn, conn);
                if (eventData != null)
                {
                    opver = BUS.OperationManager.GetOperationVersion(operationVersionID, txn, conn);
                    eventData.ForEach(item => { item.OperationId = opver == null ? Guid.Empty : opver.OperationID; });
                    eventContext.EventData = eventData;
                }
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for OperationAssociationAdded.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }

        public static DTO.DBEventContext GetEventContextForOperationUpdate(Guid operationID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {
                List<DTO.DBEventDataItem> eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForOperationID(operationID, txn, conn);
                if (eventData != null)
                    eventContext.EventData = eventData;
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for OperationUpdate.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForOperationVersionDelete(Guid operationVersionID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Delete);
            List<DTO.DBEventDataItem> eventData = null;
            try
            {
                eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForOperationVersionID(operationVersionID, txn, conn);
                if (eventData != null)
                    eventContext.EventData = eventData;
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for OperationVersionDelete.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }
        public static DTO.DBEventContext GetEventContextForOperationVersionUpdate(Guid operationVersionID, SqlTransaction txn, SqlConnection conn)
        {
            DTO.DBEventContext eventContext = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);
            List<DTO.DBEventDataItem> eventData = null;
            try
            {
                eventData = BUS.RuntimeServerManager.ListRuntimeEventDataForOperationVersionID(operationVersionID, txn, conn);
                if (eventData != null)
                    eventContext.EventData = eventData;
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for OperationVersionUpdate.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return eventContext;
        }

        public static DTO.RepositoryChangeSet GetChangeSetForPolicyUpdate(DTO.Policy policy, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersPolicyUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {

                //Get event data identifying impact scope (RuntimeServer, Endpoint, Operation)
                List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForPolicyID(policy.ID, txn, conn);
                FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);

                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(rsEvents);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for Policy update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return repositoryChangeSet;
        }
        public static DTO.RepositoryChangeSet GetChangeSetForResourceUpdate(DTO.Resource resource, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {

                //Get event data identifying impact scope (RuntimeServer, Endpoint, Operation)
                List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForResourceID(resource.ID, txn, conn);
                FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);

                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(rsEvents);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for Resource update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return repositoryChangeSet;
        }
        public static DTO.RepositoryChangeSet GetChangeSetForSystemInstanceUpdate(DTO.SystemInstance instance, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {

                //Get event data identifying impact scope (RuntimeServer, Endpoint, Operation)
                List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForSystemInstanceID(instance.ID, txn, conn);
                FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);

                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(rsEvents);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for SystemInstance update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return repositoryChangeSet;
        }
        public static DTO.RepositoryChangeSet GetChangeSetForSystemUpdate(DTO.MSESystem system, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {

                //Get event data identifying impact scope (RuntimeServer, Endpoint, Operation)
                List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForSystemID(system.ID, txn, conn);
                FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);

                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(rsEvents);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for System update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }

            return repositoryChangeSet;
        }

        public static DTO.RepositoryChangeSet GetChangeSetForPolicyAssertionUpdate(DTO.PolicyAssertion assertion, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersPolicyUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {
                //identify where policy assertion is used
                DTO.PolicyList list = DataAccess.Policy.ListPoliciesForPolicyAssertionID(assertion.ID, txn, conn);
                if (list != null && list.Policies != null)
                {
                    foreach (DTO.PolicyListElement policy in list.Policies)
                    {
                        List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForPolicyID(policy.ID, txn, conn);
                        FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);
                    }
                }
                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
                repositoryChangeSet.Items.Add(rsEvents);


            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for PolicyAssertion update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }
            return repositoryChangeSet;
        }

        public static DTO.RepositoryChangeSet GetChangeSetForAssertionTypeUpdate(DTO.AssertionType assertionType, SqlTransaction txn, SqlConnection conn)
        {
            DTO.RepositoryChangeSet repositoryChangeSet = null;
            DTO.DBEventContext rsEvents = new DTO.DBEventContext(DTO.DBEvents.RuntimeServersPolicyUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext epEvents = new DTO.DBEventContext(DTO.DBEvents.EndpointsUpdated, DTO.DBChangeType.Update);
            DTO.DBEventContext opEvents = new DTO.DBEventContext(DTO.DBEvents.OperationsUpdated, DTO.DBChangeType.Update);

            try
            {
                //identify where policy assertion is used
                DTO.PolicyList list = DataAccess.Policy.ListPoliciesForAssertionTypeID(assertionType.ID, txn, conn);
                if (list != null && list.Policies != null)
                {
                    foreach (DTO.PolicyListElement policy in list.Policies)
                    {
                        List<DTO.DBEventDataItem> data = BUS.RuntimeServerManager.ListRuntimeEventDataForPolicyID(policy.ID, txn, conn);
                        FillEventContextFromEventData(data, rsEvents, epEvents, opEvents);
                    }
                }
                repositoryChangeSet = new DTO.RepositoryChangeSet(true);
                repositoryChangeSet.Items.Add(epEvents);
                repositoryChangeSet.Items.Add(opEvents);
                repositoryChangeSet.Items.Add(rsEvents);


            }
            catch (System.Exception ex)
            {
                TraceHelper.TraceError("Error collecting affected runtimes for AssertionType update.  No change notifications will be sent. Error [{0}].", CatalogSvcHelper.GetExceptionDetails(ex));
            }
            return repositoryChangeSet;
        }

        private static void FillEventContextFromEventData(List<DTO.DBEventDataItem> dataList, DTO.DBEventContext rsEvents, DTO.DBEventContext epEvents, DTO.DBEventContext opEvents)
        {
            foreach (DTO.DBEventDataItem item in dataList)
            {
                if (item.EndpointId == Guid.Empty && item.OperationId == Guid.Empty)
                {
                    //runtime server change
                    if (!rsEvents.EventData.Contains(item))
                        rsEvents.EventData.Add(item);
                }
                else if (item.OperationId == Guid.Empty)
                {
                    //endpoint change
                    if (!epEvents.EventData.Contains(item))
                        epEvents.EventData.Add(item);
                }
                else
                {
                    //operation change (includes resources, instances, systems)
                    if (!opEvents.EventData.Contains(item))
                    {
                        opEvents.EventData.Add(item);
                    }
                }
            }

        }
    }
}
